package com.study.mike.common.converter;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.study.mike.common.enums.BookStatusEnum;

/**
 * @author KangHui
 * @Date 2023/3/24 15:33
 */
public class BookStatusConverter implements Converter<BookStatusEnum> {
    @Override
    public Class supportJavaTypeKey() {
        return BookStatusEnum.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * cellDate转对象属性，导入时转换
     */
    @Override
    public BookStatusEnum convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        String stringValue = cellData.getStringValue();
        if (ObjectUtil.equal(stringValue, BookStatusEnum.ON.getValue())) {
            return BookStatusEnum.ON;
        } else if (ObjectUtil.equal(stringValue, BookStatusEnum.OFF.getValue())) {
            return BookStatusEnum.OFF;
        } else {
            return null;
        }
    }

    /**
     * 对象转cellDate,导出时转换
     *
     * @return
     */
    @Override
    public WriteCellData<?> convertToExcelData(BookStatusEnum status, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        if (ObjectUtil.equal(status.getStatus(), 0)) {
            return new WriteCellData<>(BookStatusEnum.OFF.getValue());
        } else if (ObjectUtil.equal(status.getStatus(), 1)) {
            return new WriteCellData<>(BookStatusEnum.ON.getValue());
        } else {
            return new WriteCellData<>("");
        }
    }
}
